import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../widgets/inputWidget.dart';
import '../../model/things.dart';
import '../../helper/routeHelper.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_sms/flutter_sms.dart';
import 'package:background_sms/background_sms.dart';
import './sms_helper.dart';

class SmsSendPage extends StatefulWidget {
  final bool ifAppbar;

  @override
  SmsSendPage(this.ifAppbar);
  @override
  SmsSendPageState createState() => new SmsSendPageState();
}

class SmsSendPageState extends State<SmsSendPage> {
  List<Things> RList = <Things>[];
  List<Things> RList2 = <Things>[];

  bool isEnd = false;
  ScrollController _scrollController = new ScrollController();
  bool firstLoading = false;
  int canShow = 0;

  late Things X1;
  late String sData;

  final TextEditingController c1 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    requestSMSPermission();
  }

  Future<Null> delete(Things r1) async {
    // await addOne();
    await Things.Delete(r1);
    setState(() {
      RList.remove(r1);
    });
  }

  onDeleted(Things r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  Future<Null> getRefrush() async {
    var list1 = await Things.GetAllList();
    setState(() {
      RList = list1;
      RList2 = list1;
      c1.text = "";
    });
    // search();
  }

  Future<bool> requestSMSPermission() async {
    var status = await Permission.sms.status;
    status = await Permission.sms.request();
    if (!status.isGranted) {
      status = await Permission.sms.request();
    }
    return status.isGranted;
  }

  Future<void> sendDirectSMS(String phoneNumber, String message) async {
    String message = "This is a test message!";
    List<String> recipents = ["18884230190", "15840578312"];

    String _result =
        await sendSMS(message: message, recipients: recipents, sendDirect: true)
            .catchError((onError) {
      print(onError);
    });
    print(_result);
  }

  Future<void> sendSMS3(String number, String message) async {
    await requestSMSPermission();
    var result =
        await BackgroundSms.sendMessage(phoneNumber: number, message: message);
    if (result == SmsStatus.sent) {
      print("Sent");
    } else {
      print("Failed");
    }
  }

  void sendSMS22() async {
    if (await requestSMSPermission()) {
      bool success = await SMSHelper.sendSMS('18884230190', '测试短信内容');
      print(success ? '短信发送成功' : '短信发送失败，请检查权限或其他问题');
      // await sendSMS3('18884230190', '测试短信内容');
    } else {
      print('没有短信发送权限');
    }
  }

  void search() {
    var list1 = <Things>[];
    sData = c1.text;
    for (var item in RList) {
      if (item.TName.contains(sData) ||
          item.TType.contains(sData) ||
          item.Storage.contains(sData)) {
        list1.add(item);
      }
    }
    RList2 = list1;
    this.setState(() {});
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];

    return list1;
  }

  @override
  Widget build(BuildContext context) {
    if (RList == null) return new Container();
    // TODO: implement build
    return new Scaffold(
        appBar: widget.ifAppbar
            ? AppBar(
                title: Text('选择物资'),
                actions: <Widget>[
                  Helper.AuthPB("物资管理")
                      ? IconButton(
                          icon: Icon(
                            Icons.add,
                            color: Colors.white,
                          ),
                          onPressed: () {
                            RouteHelper.ThingsAdd(context, null, getRefrush);
                          })
                      : Container(),
                ],
              )
            : null,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.refresh),
          onPressed: () => sendSMS22(),
        ),
        body: firstLoading == true
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Column(children: getWidgetList(context)));
  }
}
